/**   
 * @Title: StringUtil.java
 * @Package com.nbst.comnutil
 * @Description:
 * @author King  
 * @date 2018-9-29  
 */

package com.nbst.comnutil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

/**
 * @ClassName: StringUtil
 * @Description:字符串操作工具
 * @author King
 * @date 2018-9-29
 * 
 */

public class StringUtil {
	/**
	 * 判段字符串是否为null和""
	 * 
	 * @param value
	 * @return boolean
	 * @author King
	 * @date 2018-9-29
	 */
	public static boolean isEmpty(@Nullable Object value) {
		if ((value == null) || "".equals(value)) {
			return true;
		}
		return false;

	}

	/**
	 * 判断字符串是否为数字
	 * 
	 * @param value
	 * @return boolean
	 * @author King
	 * @date 2018-9-29
	 */
	public static boolean isNumber(String value) {

		if ((value == null) || "".equals(value)) {
			return false;
		}
		boolean pointExists = false;
		boolean ok = true;
		for (int i = (value.startsWith("-") ? 1 : 0); i < value.length(); i++) {
			String sValue = value.substring(i, i + 1);
			if ("0123456789.".indexOf(sValue) < 0) {
				ok = false;
				break;
			} else {
				if (sValue.equals(".")) {
					if (pointExists) {
						ok = false;
						break;
					} else {
						pointExists = true;
					}
				}
			}
		}
		return ok;
	}

	/**
	 * 根据老的编号生成下一个新的编号
	 * 
	 * @param fBillNo
	 *            老编号
	 * @param model
	 *            编号的前缀
	 * @return String
	 * @author King
	 * @date 2018-9-29
	 */
	public static String nextfBillNo(String fBillNo, String model) {
		if (!StringUtils.isEmpty(fBillNo)) {
			String[] arr = fBillNo.split(model);
			if (arr.length > 0) {
				Integer n = Integer.parseInt(arr[arr.length - 1]) + 1;
				String res = String.format("%" + arr[arr.length - 1].length() + "s", n);
				String newBillNo = model + res.replaceAll("\\s", "0");
				return newBillNo;
			}
			return null;
		} else {
			return model + "00001";
		}
	}

	/**
	 * 将列表数据组装成字符串
	 * 
	 * @param list
	 *            列表
	 * @param separator
	 *            分隔符
	 * @return
	 * @throws Exception
	 *             当separator为空
	 * @author leeyu20140506
	 */
	public static String join(Collection<String> list, String separator) throws Exception {
		String str = "";
		if (separator == null) {
			separator = ",";
		}
		StringBuffer buf = new StringBuffer();
		try {
			if ((list != null) && (list.size() > 0)) {
				for (String s : list) {
					buf.append(s).append(separator);
				}
			}
			if (buf.length() >= separator.length()) {
				str = buf.substring(0, buf.length() - separator.length());
			}
		} catch (Exception ex) {
			throw ex;
		} finally {
			buf.delete(0, buf.length());
			buf = null;
		}
		return str;
	}

	/**
	 * 创建一个UUID
	 * 
	 * @return String
	 * @author King
	 * @date 2018-9-29
	 */
	public static String createUUID() {
		String ID = UUID.randomUUID().toString();
		ID = ID.replace("-", "");
		return ID;
	}

	/**
	 * 将List<Map<String, Object>> 转为 List<List<String>>
	 * 
	 * @param dataList
	 * @return
	 */
	public static List<List<String>> mapList2StringList(List<Map<String,Object>> dataList, boolean head) {

		List<List<String>> strList = new ArrayList<List<String>>();
		if (head == true) {
			for (Map<String,Object> map : dataList) {
				for (Object object : map.keySet()) {
					List<String> item = new ArrayList<String>();
					item.add(String.valueOf(object));
					strList.add(item);
				}
				break;
			}
		} else {
			for (Map<String,Object> map : dataList) {
				List<String> item = new ArrayList<String>();
				for (Object object : map.values()) {
					item.add(String.valueOf(object));
				}
				strList.add(item);
			}
		}

		return strList;
	}

}
